package org.jeecg.modules.manage.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.manage.entity.CaishuiBill;
import org.jeecg.modules.manage.service.IMCaishuiBillService;
import org.jeecg.modules.manage.service.IMCaishuiRechargeService;
import org.jeecg.modules.manage.service.IMCaishuiRefundRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 开票记录[管理]
 * @Author: yfkj
 * @Date: 2023-02-23
 * @Version: V1.0
 */
@Api(tags = "开票记录[管理]")
@RestController
@RequestMapping("/manageApi/caishuiBill")
@Slf4j
public class MCaishuiBillController extends JeecgController<CaishuiBill, IMCaishuiBillService> {

    @Autowired
    private IMCaishuiBillService imCaishuiBillService;
    @Autowired
    private IMCaishuiRechargeService imCaishuiRechargeService;
    @Autowired
    private IMCaishuiRefundRecordService imCaishuiRefundRecordService;


    /**
     * 分页列表查询
     *
     * @param caishuiBill
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "开票记录[管理]-分页列表查询", notes = "开票记录[管理]-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CaishuiBill>> queryPageList(CaishuiBill caishuiBill,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<CaishuiBill> queryWrapper = QueryGenerator.initQueryWrapper(caishuiBill, req.getParameterMap());
        Page<CaishuiBill> page = new Page<CaishuiBill>(pageNo, pageSize);
        IPage<CaishuiBill> pageList = imCaishuiBillService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 查询开票统计
     *
     * @return
     */
    @ApiOperation(value = "开票记录[管理]-查询开票统计", notes = "开票记录[管理]-查询开票统计")
    @GetMapping(value = "/queryBillStatistics")
    public Result<?> queryBillStatistics() {
        //充值总额
        Map<String, String> params = new HashMap<>();
        params.put("status","1");
        Map<String,Object> map = imCaishuiRechargeService.queryTotalAmount(params);
        double rechargeTotalAmount = oConvertUtils.getDouble(map.get("amount")+"",0.00);
        double billInAmount = 0.00;//开票中金额
        double billAlreadyAmount = 0.00; //已开票金额
        double billCanAmount = 0.00;//可开发票金额
        double refundAmount = 0.00;//退款金额
        if(rechargeTotalAmount > 0){
            billInAmount = imCaishuiBillService.queryInAmount(null,null);
            billAlreadyAmount = imCaishuiBillService.queryAlreadyAmount(null,null);
            //退款金额（退款中+退款成功）
            params.put("status","");
            params.put("inStatus","0,1,3,4");
            map = imCaishuiRefundRecordService.queryRefundTotal(params);
            refundAmount = oConvertUtils.getDouble(map.get("money")+"",0.00);
            //可开发票金额 = 充值总额 - 开票金额（开票中+开票成功） - 退款金额（退款中+退款成功）
            billCanAmount = BigDecimal.valueOf(rechargeTotalAmount).subtract(BigDecimal.valueOf(billAlreadyAmount))
                    .subtract(BigDecimal.valueOf(billInAmount)).subtract(BigDecimal.valueOf(refundAmount)).setScale(BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        JSONObject result = new JSONObject(4);
        result.put("billCanAmount",billCanAmount);
        result.put("billInAmount",billInAmount);
        result.put("billAlreadyAmount",billAlreadyAmount);
        result.put("refundAmount",refundAmount);
        return Result.OK(result);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "开票记录[管理]-通过id删除")
    @ApiOperation(value = "开票记录[管理]-通过id删除", notes = "开票记录[管理]-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        imCaishuiBillService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "开票记录[管理]-批量删除")
    @ApiOperation(value = "开票记录[管理]-批量删除", notes = "开票记录[管理]-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.imCaishuiBillService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "开票记录[管理]-通过id查询", notes = "开票记录[管理]-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<CaishuiBill> queryById(@RequestParam(name = "id", required = true) String id) {
        CaishuiBill caishuiBill = imCaishuiBillService.getById(id);
        if (caishuiBill == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(caishuiBill);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param caishuiBill
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CaishuiBill caishuiBill) {
        return super.exportXls(request, caishuiBill, CaishuiBill.class, "开票记录[管理]");
    }

}
